// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) braces deadcode fieldsfirst 

package net.minecraft;

import java.applet.Applet;
import java.applet.AppletStub;
import java.awt.*;
import java.awt.image.VolatileImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;

// Referenced classes of package net.minecraft:
//            LoginForm, GameUpdater

public class Launcher extends Applet
    implements Runnable, AppletStub
{

    private static final long serialVersionUID = 1L;
    public Map<String, String> customParameters;
    private GameUpdater gameUpdater;
    private boolean gameUpdaterStarted;
    private Applet applet;
    private Image bgImage;
    private boolean active;
    private int context;
    private VolatileImage img;
    public boolean forceUpdate;

    public Launcher()
    {
        customParameters = new HashMap<String, String>();
        gameUpdaterStarted = false;
        active = false;
        context = 0;
        forceUpdate = false;
    }

    public boolean isActive()
    {
        if(context == 0)
        {
            context = -1;
            try
            {
                if(getAppletContext() != null)
                {
                    context = 1;
                }
            }
            catch(Exception exception) { }
        }
        if(context == -1)
        {
            return active;
        } else
        {
            return super.isActive();
        }
    }

    public void init(String s, String s1)
    {
        try
        {
            bgImage = ImageIO.read((net.minecraft.LoginForm.class).getResource("dirt.png")).getScaledInstance(32, 32, 16);
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }
        customParameters.put("username", s);
        customParameters.put("sessionid", s1);
        gameUpdater = new GameUpdater();
        gameUpdater.forceUpdate = forceUpdate;
    }

    public boolean canPlayOffline()
    {
        return gameUpdater.canPlayOffline();
    }

    public void init()
    {
        if(applet != null)
        {
            applet.init();
            return;
        } else
        {
            init(getParameter("userName"), getParameter("sessionId"));
            return;
        }
    }

    public void start()
    {
        if(applet != null)
        {
            applet.start();
            return;
        }
        if(gameUpdaterStarted)
        {
            return;
        } else
        {
            Thread thread = new Thread() {

                public void run()
                {
                    gameUpdater.run();
                    try
                    {
                        if(!gameUpdater.fatalError)
                        {
                            replace(gameUpdater.createApplet());
                        }
                    }
                    catch(ClassNotFoundException classnotfoundexception)
                    {
                        classnotfoundexception.printStackTrace();
                    }
                    catch(InstantiationException instantiationexception)
                    {
                        instantiationexception.printStackTrace();
                    }
                    catch(IllegalAccessException illegalaccessexception)
                    {
                        illegalaccessexception.printStackTrace();
                    }
                }

            }
;
            thread.setDaemon(true);
            thread.start();
            thread = new Thread() {

                public void run()
                {
                    while(applet == null) 
                    {
                        repaint();
                        try
                        {
                            Thread.sleep(10L);
                        }
                        catch(InterruptedException interruptedexception)
                        {
                            interruptedexception.printStackTrace();
                        }
                    }
                }

            }
;
            thread.setDaemon(true);
            thread.start();
            gameUpdaterStarted = true;
            return;
        }
    }

    public void stop()
    {
        if(applet != null)
        {
            active = false;
            applet.stop();
            return;
        } else
        {
            return;
        }
    }

    public void destroy()
    {
        if(applet != null)
        {
            applet.destroy();
            return;
        } else
        {
            return;
        }
    }

    public void replace(Applet applet1)
    {
        applet = applet1;
        applet1.setStub(this);
        applet1.setSize(getWidth(), getHeight());
        setLayout(new BorderLayout());
        add(applet1, "Center");
        applet1.init();
        active = true;
        applet1.start();
        validate();
    }

    public void update(Graphics g)
    {
        paint(g);
    }

    public void paint(Graphics g)
    {
        if(applet != null)
        {
            return;
        }
        int i = getWidth() / 2;
        int j = getHeight() / 2;
        if(img == null || img.getWidth() != i || img.getHeight() != j)
        {
            img = createVolatileImage(i, j);
        }
        Graphics g1 = img.getGraphics();
        for(int k = 0; k <= i / 32; k++)
        {
            for(int l = 0; l <= j / 32; l++)
            {
                g1.drawImage(bgImage, k * 32, l * 32, null);
            }

        }

        g1.setColor(Color.LIGHT_GRAY);
        String s = "Updating Minecraft";
        if(gameUpdater.fatalError)
        {
            s = "Failed to launch";
        }
        g1.setFont(new Font(null, 1, 20));
        FontMetrics fontmetrics = g1.getFontMetrics();
        g1.drawString(s, i / 2 - fontmetrics.stringWidth(s) / 2, j / 2 - fontmetrics.getHeight() * 2);
        g1.setFont(new Font(null, 0, 12));
        fontmetrics = g1.getFontMetrics();
        s = gameUpdater.getDescriptionForState();
        if(gameUpdater.fatalError)
        {
            s = gameUpdater.fatalErrorDescription;
        }
        g1.drawString(s, i / 2 - fontmetrics.stringWidth(s) / 2, j / 2 + fontmetrics.getHeight() * 1);
        s = gameUpdater.subtaskMessage;
        g1.drawString(s, i / 2 - fontmetrics.stringWidth(s) / 2, j / 2 + fontmetrics.getHeight() * 2);
        if(!gameUpdater.fatalError)
        {
            g1.setColor(Color.black);
            g1.fillRect(64, j - 64, (i - 128) + 1, 5);
            g1.setColor(new Color(32768));
            g1.fillRect(64, j - 64, (gameUpdater.percentage * (i - 128)) / 100, 4);
            g1.setColor(new Color(0x20a020));
            g1.fillRect(65, (j - 64) + 1, (gameUpdater.percentage * (i - 128)) / 100 - 2, 1);
        }
        g1.dispose();
        g.drawImage(img, 0, 0, i * 2, j * 2, null);
    }

    public void run()
    {
    }

    public String getParameter(String s)
    {
        String s1 = (String)customParameters.get(s);
        if(s1 != null)
        {
            return s1;
        }
        try
        {
            return super.getParameter(s);
        }
        catch(Exception exception)
        {
            customParameters.put(s, null);
        }
        return null;
    }

    public void appletResize(int i, int j)
    {
    }

    public URL getDocumentBase()
    {
        try
        {
            return new URL("http://www.youtube.com/user/anjocaido0");
        }
        catch(MalformedURLException malformedurlexception)
        {
            malformedurlexception.printStackTrace();
        }
        return null;
    }


}
